// pages/seat_lib/seat_detail_tea/seat_detail_tea.js
const baseURL = 'http://100.2.178.56:8080'
var date = new Date();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: null,
        seatDetail: null,
        reserve_detail_list: null,

        fileList: [], //图片存放的数组
    },
    cancel_click: function (e) {
        var that = this
        wx.request({
            url: baseURL + '/studentSeatRelation/powerCancel',
            header: {
                'Authorization': getApp().token
            },
            data: {
                "stuUseId": e.target.dataset.value,
            },
            success: function (res) {
                if (res.data.code == 0) {
                    wx.switchTab({
                        url: '/pages/seat_lib/seat_lib'
                    })
                }
            }
        })
    },

    async upload_click() {
        let that = this;
        // let timestamp = (new Date()).valueOf();
        var cont1 = await wx.chooseImage({})
        wx.showLoading({
            title: '上传中。。。',
        })
        // 将图片上传至云存储空间
        var cont2 = await wx.cloud.uploadFile({
            // 指定上传到的云路径
            cloudPath: that.data.seatDetail.seat.seaNum + '_' + that.data.seatDetail.floNum + '.png',
            // 指定要上传的文件的小程序临时文件路径
            filePath: cont1.tempFilePaths[0],
        })
        console.log(cont2)
        // 成功回调
        console.log('上传成功', cont2)
        wx.hideLoading()
        wx.showToast({
            title: '上传图片成功',
        })
        if (cont2.statusCode == 204) {

            that.setData({
                //   zhaopian: '图片如下',
                imgUrl: cont2.fileID
            })
            console.log('上传成功', that.data.imgUrl)
            console.log('上传成功', that.data.imgUrl)
            var realUrl = null
            realUrl = await that.addImagePath(that.data.imgUrl)
            console.log(realUrl)

            wx.request({
                url: baseURL + '/seat/upload',
                method: 'GET',
                header: {
                    'Authorization': getApp().token
                },
                data: {
                    "seaId": that.data.seatDetail.seat.seaId,
                    "fileID": realUrl

                },
                success: function (res) {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 3000
                    })
                }

            })
        }



    },
    // 获取图片上传后的https的url路径地址  参数是上传图片的 fileId
    async addImagePath(fileId) {
        var realUrl = null
        await wx.cloud.getTempFileURL({
                fileList: [fileId]
            })
            .then(res => {
                console.log("获取url地址：", res.fileList[0].tempFileURL);
                realUrl = res.fileList[0].tempFileURL
                console.log('realUrl', realUrl)
                return realUrl
            })
        return realUrl
        // fail: console.error

    },
    // ======================================================================
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        console.log("onLoad->:", options)
        var detail = JSON.parse(options.detail)
        this.setData({
            seatDetail: detail,
            imgUrl: detail.seat.seaPicture
        })

        if (detail.reserveState.resStaId == 2) {

            var date_reserve;
            var datee = new Date()
            if (that.data.seatDetail.tomorrow) {
                //明天
                var date1 = new Date(datee);
                date1.setDate(date.getDate() + 1);

                var monthDay = that.convertMonthDay(date1.getMonth() + 1, date1.getDate())
                date_reserve = date.getFullYear() + "-" + monthDay;
            } else {
                //今天
                var month = datee.getMonth() + 1;
                var day = datee.getDate();
                monthDay = that.convertMonthDay(month, day);
                date_reserve = date.getFullYear() + "-" + monthDay;
            }
            console.log(date_reserve)
            wx.request({
                url: baseURL + '/studentSeatRelation/seaStaId',
                header: {
                    'Authorization': getApp().token
                },
                data: {

                    "seaStaId": that.data.seatDetail.seaStaId,
                    "resStartTime": date_reserve

                },
                success: function (res) {
                    if (res.data.code == 0) {
                        that.setData({
                            reserve_detail_list: res.data.data.reserveList
                        })
                        console.log(" res.data.data.reserveList:", res.data.data.reserveList)


                    }

                }
            })
        }
    },
    convertMonthDay: function (month, day) {
        var mon;
        var da;
        if (month < 10) {
            mon = '0' + month
        } else {
            mon = month
        }
        if (day < 10) {
            da = '0' + day
        } else {
            da = day
        }
        return mon + '-' + da
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})